package com.dy.分类.栈队列堆.队列实现栈;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public static void main(String[] args) {
        MyStack2 myStack2 = new MyStack2();
        myStack2.push(1);
        myStack2.push(2);
        myStack2.push(3);
        while(!myStack2.empty()){
            System.out.println(myStack2.pop());
        }
    }
}
class MyStack {
    Queue<Integer> data;
    Queue<Integer> tmp;
    /** Initialize your data structure here. */
    public MyStack() {
        data = new LinkedList();
        tmp = new LinkedList();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        tmp.offer(x);
        while(!data.isEmpty()){
            tmp.offer(data.poll());
        }
        while (!tmp.isEmpty()){
            data.offer(tmp.poll());
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return data.poll();
    }

    /** Get the top element. */
    public int top() {
        return data.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return data.isEmpty();
    }


}
class MyStack2 {
    Queue<Integer> data;
    Integer top;
    /** Initialize your data structure here. */
    public MyStack2() {
        data = new LinkedList();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        data.offer(x);
        top=x;
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        int size = data.size();
        while(size>2){
            data.offer(data.poll());
            size--;
        }
        top =data.poll();
        data.offer(top);
        return data.poll();
    }

    /** Get the top element. */
    public int top() {
        return top;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return data.isEmpty();
    }


}

